package com.lungle.warmdiary.app.view.message.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseListFragment;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.request.MessagePost;
import com.lungle.warmdiary.app.model.response.Message;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.message.adapter.MessageAdapter;
import com.trello.rxlifecycle.FragmentEvent;
import com.umeng.analytics.MobclickAgent;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author sbbic
 *         created at 2016/9/12 11:23
 */
public class MessageCenterFragment extends BaseListFragment<Message> {
    private static final String MESSAGE_CENTER_SCREEN = "message center screen";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mErrorLayout.setNoDataContent(String.format(getResources().getString(R.string.empty_message_list)));
        MobclickAgent.onPageStart(MESSAGE_CENTER_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(MESSAGE_CENTER_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    @Override
    public void initView(View view) {
        super.initView(view);
    }

    @Override
    public void initData() {
        super.initData();
    }


    @Override
    protected ListBaseAdapter getListAdapter() {
        MessageAdapter adapter = new MessageAdapter();

        return adapter;
    }


    @Override
    protected void sendRequestData() {
        ApiFactory.getUserApi().getMessageList(mCurrentPage * getPageSize(), getPageSize()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<Result<?>>bindUntilEvent(FragmentEvent.STOP))
                .subscribe(mSubscriber);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Message message = mAdapter.getItem(position);
        if (message == null) return;

        // TODO: 16/9/25 改变已读状态，如果是已读，不在发送
        if (message.getStatus() != Message.STATE_READ)
            setReaded(message.getId());
        if (message.getLink() != null) {
            //跳转到列表相关
            UIHelper.dispatchAction(getActivity(), message.getLink());
        } else {
            //跳转到详情相关
            UIHelper.showMessageDetail(getActivity(), message);
        }
        message.setStatus(Message.STATE_READ);
        mAdapter.notifyDataSetChanged();

    }

    private void setReaded(String id) {
        MessagePost messagePost = new MessagePost(id);
        ApiFactory.getUserApi().updateMessageState(messagePost).subscribeOn(Schedulers.io())
                .subscribe(new BaseSubscriber<Result>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onNext(Result result) {
                    }
                });
    }


}
